package binarytree;

import javax.lang.model.util.Elements;
import javax.swing.*;

public class TestBinaryTree {

    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode creatTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;

        return A;  //根节点
    }

    // 前序遍历
    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        //直接打印跟节点的值
        System.out.print(root.val + " ");
        //递归遍历左子树
        preOrder(root.left);
        //递归遍历右子树
        preOrder(root.right);
    }


    // 中序遍历
    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        //递归遍历左子树
        inOrder(root.left);
        //打印跟节点的值
        System.out.print(root.val + " ");
        //递归遍历右子树
        inOrder(root.right);
    }


    // 后序遍历
    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }


    //获取节点个数
    public int size(TreeNode root) {
        if (root == null) {
            return 0;
        }
        //节点个数 = 左子树节点个数 + 右子树节点个数 + 1
        int ret = size(root.left) + size(root.right) + 1;
        return ret;
    }


    public static int nodeSize;

    public void size2(TreeNode root) {
        if (root == null) {
            return;
        }
        nodeSize++;
        //左树中的节点
        size2(root.left);
        //右树中的节点
        size2(root.right);
    }


    /**
     * 子问题思路
     * 求叶子节点的个数
     *
     * @return
     */
    public int getLeafNodeCount(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
    }

    /**
     * 遍历思路
     * 求叶子节点个数
     */
    public int leafSize;

    public void getLeafNodeCount2(TreeNode root) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            leafSize++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
    }


    //第k层有多少个节点
    public int getKLevelNodeCount(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left, k - 1) +
                getKLevelNodeCount(root.right, k - 1);
    }


    //获取二叉树的高度
    public int getHight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftHight = getHight(root.left);
        int rightHight = getHight(root.right);

        return leftHight > rightHight ? leftHight + 1 : rightHight + 1;
    }



    public TreeNode find(TreeNode root, char val) {
        if (root == null) {
            return null;
        }
        //第一个节点就是要找的目标
        if (root.val == val) {
            return root;
        }
        //遍历每棵左子树寻找
        TreeNode ret = find(root.left, val);
        //只要返回值不为空，就是找到了
        if (ret != null) {
            return ret;
        }
        //遍历每棵右子树寻找
        ret = find(root.right, val);
        if (ret != null) {
            return ret;
        }
        return null;
    }

}





